import type { AgentsStore } from './index';

// Agents Store选择器
export const agentsSelectors = {
  // 联网搜索相关选择器
  webSearchEnabled: (state: AgentsStore) => state.webSearchEnabled,
  webSearchSettings: (state: AgentsStore) => state.webSearchSettings,
  webSearchProvider: (state: AgentsStore) => state.webSearchSettings.provider,
  
  // 文件选择相关选择器
  selectedFiles: (state: AgentsStore) => state.selectedFiles,
  selectedFileCount: (state: AgentsStore) => state.selectedFiles.length,
  maxSelectedFiles: (state: AgentsStore) => state.maxSelectedFiles,
  canAddMoreFiles: (state: AgentsStore) => 
    state.selectedFiles.length < state.maxSelectedFiles,
  isMaxFilesReached: (state: AgentsStore) => 
    state.selectedFiles.length >= state.maxSelectedFiles,
  
  // 设置相关选择器
  settings: (state: AgentsStore) => state.settings,
  isSettingsLoaded: (state: AgentsStore) => state.isSettingsLoaded,
  settingsVersion: (state: AgentsStore) => state.settingsVersion,
  
  // 复合选择器
  hasAnyToolsActive: (state: AgentsStore) => 
    state.webSearchEnabled || state.selectedFiles.length > 0,
  
  toolsSummary: (state: AgentsStore) => ({
    webSearch: {
      enabled: state.webSearchEnabled,
      provider: state.webSearchSettings.provider,
    },
    fileSelection: {
      count: state.selectedFiles.length,
      maxFiles: state.maxSelectedFiles,
      files: state.selectedFiles.map(f => ({
        id: f.id,
        name: f.name,
        type: f.type,
      })),
    },
  }),

  // 文件相关选择器
  getSelectedFileById: (fileId: string) => (state: AgentsStore) =>
    state.selectedFiles.find(file => file.id === fileId),
  
  isFileSelected: (fileId: string) => (state: AgentsStore) =>
    state.selectedFiles.some(file => file.id === fileId),
    
  getFilesByType: (fileType: string) => (state: AgentsStore) =>
    state.selectedFiles.filter(file => file.type === fileType),
  
  getTotalFilesSize: (state: AgentsStore) =>
    state.selectedFiles.reduce((total, file) => total + file.size, 0),
    
  // UI相关选择器
  showFilePreview: (state: AgentsStore) => state.settings.ui.showFilePreview,
  compactMode: (state: AgentsStore) => state.settings.ui.compactMode,
  
  // 文件选择限制选择器
  allowedFileTypes: (state: AgentsStore) => state.settings.fileSelection.allowedTypes,
  maxFileSize: (state: AgentsStore) => state.settings.fileSelection.maxFileSize,
  
  // 验证选择器
  canSelectFile: (file: { type: string; size: number }) => (state: AgentsStore) => {
    const { allowedTypes, maxFileSize } = state.settings.fileSelection;
    const isTypeAllowed = allowedTypes.some(type => 
      file.type.endsWith(type) || file.type === type
    );
    const isSizeAllowed = file.size <= maxFileSize;
    const hasSpace = state.selectedFiles.length < state.maxSelectedFiles;
    
    return isTypeAllowed && isSizeAllowed && hasSpace;
  },
};

// 导出默认选择器
export default agentsSelectors; 